using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;

namespace SJBotGame.SJBotGameComponentsDraw
{
    public class SJBotTitleMoveDraw
    {
        private SJBotTitleDraw titleDraw1;
        private SJBotTitleDraw titleDraw2;
        private Rectangle rect1;
        private Rectangle rect2;
        //
        private int lastUpdateStatus;
        private Stopwatch updateTimer = new Stopwatch();
        private double lastUpdateTimer;

        private const double timeOut = 100;
        private const int statusMax = 5;
        //
        public SJBotTitleMoveDraw()
        {
            this.titleDraw1 = null;
            this.titleDraw2 = null;
        }
        //
        public void StartMove(SJBotTitleDraw titleDraw1, SJBotTitleDraw titleDraw2)
        {
            this.titleDraw1 = titleDraw1;
            this.titleDraw2 = titleDraw2;
            this.rect1 = titleDraw1.Rect;
            this.rect2 = titleDraw2.Rect;
            //
            lastUpdateStatus = 0;
            lastUpdateTimer = 0.00;
            updateTimer.Start();
        }
        //
        private void IncStatus()
        {
            lastUpdateStatus++;
            if (lastUpdateStatus > statusMax)
            {
                lastUpdateStatus = 0;
                updateTimer.Stop();
                this.titleDraw1 = null;
                this.titleDraw2 = null;
            }
        }
        public void Update()
        {
            if ( (titleDraw1 == null) || (titleDraw2 == null) ) return;
            //
            if ((lastUpdateTimer + timeOut) < updateTimer.ElapsedMilliseconds)
            {
                lastUpdateTimer = updateTimer.ElapsedMilliseconds;
                IncStatus();
            }
        }
        public void Redraw(Graphics formGraphics)
        {
            if ((titleDraw1 == null) || (titleDraw2 == null)) return;
            //
            Rectangle _rect1 = new Rectangle(titleDraw1.Rect.X, titleDraw1.Rect.Y, titleDraw1.Rect.Width, titleDraw1.Rect.Height);
            _rect1.Inflate(1, 1);
            Rectangle _rect2 = new Rectangle(titleDraw2.Rect.X, titleDraw2.Rect.Y, titleDraw2.Rect.Width, titleDraw2.Rect.Height);
            _rect2.Inflate(1, 1);

            SolidBrush selectedBrush = new SolidBrush(SystemColors.Control);
            formGraphics.FillRectangle(selectedBrush, _rect1);
            formGraphics.FillRectangle(selectedBrush, _rect2);
            selectedBrush.Dispose();
            //
            _rect1 = new Rectangle(rect1.X, rect1.Y, rect1.Width, rect1.Height);
            _rect2 = new Rectangle(rect2.X, rect2.Y, rect2.Width, rect2.Height);
            _rect1.X = rect1.X + (rect2.X - rect1.X) * lastUpdateStatus / statusMax;
            _rect1.Y = rect1.Y + (rect2.Y - rect1.Y) * lastUpdateStatus / statusMax;
            _rect2.X = rect2.X - (rect2.X - rect1.X) * lastUpdateStatus / statusMax;
            _rect2.Y = rect2.Y - (rect2.Y - rect1.Y) * lastUpdateStatus / statusMax;
            //
            titleDraw1.Rect = _rect1;
            titleDraw2.Rect = _rect2;
            titleDraw1.Draw(formGraphics);
            titleDraw2.Draw(formGraphics);
        }
    }
}
